﻿using System;

namespace ConsoleDemo
{
    /// <summary>
    /// 异常
    /// </summary>
    public class ExceptionDemo
    {
        /// <summary>
        /// 搜索 栈 以寻找一个异常处理程序
        /// </summary>
        public static void SearchStackForExcepHandler()
        {
            MyExcepClass MCls = new MyExcepClass();
            try
            {
                MCls.A();
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("catch clause in Main()");
            }
            finally
            {
                Console.WriteLine("finally clause in Main()");
            }

            Console.WriteLine("After try statement in Main.");
            Console.WriteLine("            -- Keep running.");
        }

        /// <summary>
        /// 不带异常对象的抛出
        /// </summary>
        public static void ThrowWithoutExceptionObject()
        {
            string s = null;
            MyExcepClass MCls = new MyExcepClass();
            MCls.PrintArg(s);
        }
    }

    class MyExcepClass
    {
        public void A()
        {
            try
            {
                B();
            }
            catch (System.NullReferenceException)
            {
                Console.WriteLine("catch clause in A()");
            }
            finally
            {
                Console.WriteLine("finally clause in A()");
            }
        }

        void B()
        {
            int x = 10, y = 0;
            try
            {
                x /= y;
            }
            catch (System.IndexOutOfRangeException)
            {
                Console.WriteLine("catch clause in B()");
            }
            finally
            {
                Console.WriteLine("finally clause in B()");
            }
        }

        public void PrintArg(string arg)
        {
            try
            {
                try
                {
                    if (arg == null)
                    {
                        ArgumentNullException myEx = new ArgumentNullException("arg");
                        throw myEx;
                    }
                    Console.WriteLine(arg);
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("Inner Catch: {0}", e.Message);
                    Console.WriteLine("------c1--------------------");
                    Console.WriteLine(e.StackTrace);
                    Console.WriteLine(e.InnerException);
                    Console.WriteLine(e.Source);
                    Console.WriteLine("------c1--------------------");
                    throw;
                }
            }
            catch (Exception x)
            {
                Console.WriteLine("Outer Catch: Handling an Exception.");
                Console.WriteLine("------c2--------------------");
                Console.WriteLine(x.StackTrace);
                Console.WriteLine(x.InnerException);
                Console.WriteLine(x.Source);
                Console.WriteLine("------c2--------------------");
            }
        }
    }
}
